﻿Public Class KundeProjektTaskServiceImpl
    Inherits ServiceImpl(Of KundeProjektTaskModelListener)
    Implements KundeProjektTaskService
    Implements KundePersistenceListener
    Implements ProjektPersistenceListener
    Implements TaskPersistenceListener
    Implements ZeitrapportPersistenceListener

    Public Sub New()
        Context.KUNDE_SERVICE.getObservable().addListener(Me)
        Context.PROJEKT_SERVICE.getObservable().addListener(Me)
        Context.TASK_SERVICE.getObservable().addListener(Me)
        Context.ZEITRAPPORT_SERVICE.getObservable.addListener(Me)
    End Sub

    Private kunden As List(Of Kunde)
    Private selectedKunde As Kunde
    Private selectedProjekt As Projekt
    Private selectedTask As Task

    Public Sub setKunde(ByRef kunde As Kunde) Implements KundeProjektTaskService.setKunde
        Me.selectedKunde = kunde
        fireSelectedKundeChange(Me.selectedKunde)
    End Sub

    Public Sub setProjekt(ByRef projekt As Projekt) Implements KundeProjektTaskService.setProjekt
        Me.selectedProjekt = projekt
        fireSelectedProjektChange(Me.selectedProjekt)
    End Sub

    Public Sub setTask(ByRef task As Task) Implements KundeProjektTaskService.setTask
        Me.selectedTask = task
        fireSelectedTaskChange(Me.selectedTask)
    End Sub

    Private Sub refreshKunden()
        Connection.open()
        kunden = Context.KUNDE_SERVICE.findAll()
        Connection.close()
        fireKundenChange(kunden)
    End Sub

    ' fires
    Private Sub fireKundenChange(ByRef kunden As List(Of Kunde))
        For Each listener As KundeProjektTaskModelListener In getObservable.getListeners
            listener.onKundenChanged(kunden)
        Next
    End Sub

    Private Sub fireSelectedKundeChange(ByRef kunde As Kunde)
        For Each listener As KundeProjektTaskModelListener In getObservable.getListeners()
            listener.onSelectedKundeChanged(kunde)
        Next
    End Sub

    Private Sub fireSelectedProjektChange(ByRef projekt As Projekt)
        For Each listener As KundeProjektTaskModelListener In getObservable.getListeners
            listener.onSelectedProjektChanged(projekt)
        Next
    End Sub

    Private Sub fireSelectedTaskChange(ByRef task As Task)
        For Each listener As KundeProjektTaskModelListener In getObservable().getListeners()
            listener.onSelectedTaskChanged(Me.selectedTask)
        Next
    End Sub

    ' listener events
    Public Sub onKundeDeleted(ByRef entity As Kunde) Implements KundePersistenceListener.onDeleted
        kunden.Remove(entity)
        fireKundenChange(kunden)
    End Sub

    Public Sub onProjektDeleted(ByRef entity As Projekt) Implements ProjektPersistenceListener.onDeleted
        If entity.kunde.Equals(selectedKunde) Then
            selectedKunde.projekte.Remove(entity)
            fireSelectedKundeChange(selectedKunde)
        End If
    End Sub

    Public Sub onTaskDeleted(ByRef entity As Task) Implements TaskPersistenceListener.onDeleted
        If entity.projekt.Equals(selectedProjekt) Then
            selectedProjekt.tasks.Remove(entity)
            fireSelectedProjektChange(selectedProjekt)
        End If
    End Sub

    Public Sub onZeitrapportDeleted(ByRef entity As Zeitrapport) Implements PersistenceListener(Of Zeitrapport).onDeleted

    End Sub

    Public Sub onKundeSaved(ByRef entity As Kunde) Implements PersistenceListener(Of Kunde).onSaved
        ListUtil.updateOrAddToList(Me.kunden, entity)
        fireKundenChange(kunden)
    End Sub

    Public Sub onProjektSaved(ByRef entity As Projekt) Implements PersistenceListener(Of Projekt).onSaved
        ListUtil.updateOrAddToList(Me.selectedKunde.projekte, entity)
        fireSelectedKundeChange(selectedKunde)
    End Sub

    Public Sub onTaskSaved(ByRef entity As Task) Implements PersistenceListener(Of Task).onSaved
        ListUtil.updateOrAddToList(Me.selectedProjekt.tasks, entity)
        fireSelectedProjektChange(selectedProjekt)
    End Sub

    Public Sub onZeitrapportSaved(ByRef entity As Zeitrapport) Implements PersistenceListener(Of Zeitrapport).onSaved
        entity.task.zeitrapporte.Add(entity)
        ListUtil.updateOrAddToList(Me.selectedKunde.projekte, entity.task.projekt)
        fireSelectedKundeChange(Me.selectedKunde)
    End Sub

    Public Sub initData() Implements KundeProjektTaskService.initData
        Me.refreshKunden()
    End Sub


End Class
